"""LangGraph single-node graph template.

Returns a predefined response. Replace logic and configuration as needed.
"""

from __future__ import annotations

from langchain_core.messages import AnyMessage
from langchain_core.runnables import RunnableConfig
from langgraph.prebuilt import create_react_agent
from langgraph.prebuilt.chat_agent_executor import AgentState

from agent.my_llm import llm
from agent.my_state import CustomState
from agent.tools.tools_demo4 import calculate4
from agent.tools.tools_demo6 import runnable_tool
from agent.tools.tools_demo7 import MySearchTool
from agent.tools.tools_demo8 import get_user_info_by_name
from agent.tools.tools_demo9 import get_user_name, greet_user


# 创建一个网络搜索的工具
search_tool = MySearchTool()


# 提示词模板的函数: 由用户传入内容，组成一个动态的系统提示词
def prompt(state: AgentState, config: RunnableConfig) -> list[AnyMessage]:
    user_name = config['configurable'].get('user_name', 'gary')
    print("当前config配置中获取到的配置，user_name={}", user_name)
    system_message = f'你是一个智能助手，尽可能的调用工具回答用户的问题，当前用户的名字是: {user_name}'
    return [{"role": "system", "content": system_message}] + state['messages']


# Define the graph
graph = create_react_agent(
    llm,
    tools=[calculate4, runnable_tool, search_tool, get_user_info_by_name, get_user_name, greet_user],
    # tools=[calculate4, runnable_tool, search_tool, get_user_name, greet_user],
    # prompt="你是一个私人助手"
    prompt=prompt,   # Callable 可调用类型函数传入
    state_schema=CustomState
)
